package com.wlyuan.open.application.service;

import cn.hutool.core.collection.CollectionUtil;
import com.wlyuan.open.api.IPushService;
import com.wlyuan.open.domain.PushTask;
import com.wlyuan.open.domain.repository.PushTaskRepository;
import com.wlyuan.open.domain.service.DefaultPushStrategy;
import com.wlyuan.open.domain.service.PushStrategy;
import com.wlyuan.open.domain.service.dingtong.DingTongPushStrategy;
import com.wlyuan.open.domain.service.jiajia.JiaJiaPushStrategy;
import com.wlyuan.open.domain.service.qifan.QiFanPushStrategy;
import com.wlyuan.open.domain.service.yuhong.YuHongPushStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@DubboService
public class PushService implements IPushService {

    private final PushTaskRepository pushTaskRepository;
    private final DefaultPushStrategy defaultPushStrategy;
    private final DingTongPushStrategy dingTongPushStrategy;
    private final QiFanPushStrategy qiFanPushStrategy;
    private final YuHongPushStrategy yuHongPushStrategy;
    private final JiaJiaPushStrategy jiaJiaPushStrategy;

    private final Map<String, PushStrategy> strategies = new HashMap<>();

    @PostConstruct
    public void initialize() {
        strategies.put(PushStrategy.DEFAULT, defaultPushStrategy);
        strategies.put(PushStrategy.DINGTONG, dingTongPushStrategy);
        strategies.put(PushStrategy.QIFAN, qiFanPushStrategy);
        strategies.put(PushStrategy.YUHONG, yuHongPushStrategy);
        strategies.put(PushStrategy.JIAJIA,jiaJiaPushStrategy);
    }

    public void push(Long page,Long size){
        var pushTasks = pushTaskRepository.listNeedPush(page, size);
        //循环推送
        for (PushTask pushTask : pushTasks) {
            var strategy = strategies.get(pushTask.getPushStrategy());
            if(null!=strategy){
                strategy.handle(pushTask);
            }
        }
    }

    public void push(List<Long> taskIds){
        if(CollectionUtil.isNotEmpty(taskIds)) {
            List<PushTask> pushTasks = pushTaskRepository.listByIds(taskIds);
            if (CollectionUtil.isNotEmpty(pushTasks)) {
                //循环推送
                for (PushTask pushTask : pushTasks) {
                    var strategy = strategies.get(pushTask.getPushStrategy());
                    if (null != strategy) {
                        strategy.handle(pushTask);
                    }
                }
            }
        }
    }

}
